package com.info33.platform.system.controller;

import com.info33.platform.common.mvc.controller.BaseController;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.common.validated.SaveValidated;
import com.info33.platform.common.validated.UpdateValidated;
import com.info33.platform.system.dto.SysTenantMenuDTO;
import com.info33.platform.system.entity.SysTenantMenu;
import com.info33.platform.system.service.SysTenantMenuService;
import com.info33.platform.system.vo.SysMenuListVO;
import com.info33.platform.system.vo.SysTenantMenuDetailVO;
import com.info33.platform.system.vo.SysTenantMenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author admin
 */
@RestController
@RequestMapping("/SysTenantMenu")
@Api(tags = "系统 - 租户菜单")
public class SysTenantMenuController extends BaseController<SysTenantMenuService, SysTenantMenu> {

    private final SysTenantMenuService sysTenantMenuService;

    @Autowired
    public SysTenantMenuController(SysTenantMenuService sysTenantMenuService) {
        super(sysTenantMenuService);
        this.sysTenantMenuService = sysTenantMenuService;
    }

    @GetMapping("/getButtonMenus/{action}")
    @ApiOperation(value = "获取当前登录人按钮权限")
    public Result<List<SysTenantMenuVO>> getButtonMenuList(@PathVariable String action) {
        return Result.ok(sysTenantMenuService.getButtonMenuList(action, getCurrentUser()));
    }

    @PostMapping("/removeMenu")
    @ApiOperation(value = "删除菜单信息", notes = "删除菜单信息")
    public Result<?> removeMenu(@RequestBody List<Long> ids) {
        return sysTenantMenuService.removeMenu(getTenantId(), ids);
    }

    @PostMapping("/updateMenu")
    @ApiOperation(value = "更新菜单信息")
    public Result<?> updateMenu(@Validated(value = {SysTenantMenuDTO.UpdateMenu.class, UpdateValidated.class}) @RequestBody SysTenantMenu sysTenantMenu) {
        return sysTenantMenuService.updateMenu(getTenantId(), sysTenantMenu);
    }

    @PostMapping("/saveMenu")
    @ApiOperation(value = "添加菜单信息")
    public Result<?> saveMenu(@Validated(value = {SysTenantMenuDTO.SaveMenu.class, SaveValidated.class}) @RequestBody SysTenantMenuDTO sysTenantMenuDTO) {
        return sysTenantMenuService.saveMenu(getTenantId(), sysTenantMenuDTO);
    }

    @GetMapping(value = "/getMenuDetail/{id}")
    @ApiOperation(value = "获取菜单信息详情")
    public Result<SysTenantMenuDetailVO> getMenuDetail(@PathVariable Long id) {
        return Result.ok(sysTenantMenuService.getMenuDetail(id));
    }

    @GetMapping(value = "/pageMenuList/{pid}/{pageNum}/{pageSize}")
    @ApiOperation(value = "获取菜单分页列表")
    public Result<SysMenuListVO> pageMenuList(@PathVariable Long pid, @PathVariable int pageNum, @PathVariable int pageSize) {
        return Result.ok(sysTenantMenuService.pageMenuList(getTenantId(), pid, pageNum, pageSize));
    }

    @GetMapping(value = "/pc/menu/tree")
    @ApiOperation(value = "pc - 菜单树")
    public Result<List<SysTenantMenuVO>> pcMenuTree() {
        return Result.ok(sysTenantMenuService.pcMenuTree(getCurrentUser()));
    }

    @GetMapping(value = "/getAllMenuTree")
    @ApiOperation(value = "获取所有菜单树结构")
    public Result<List<SysTenantMenuVO>> getAllMenuTree() {
        return Result.ok(sysTenantMenuService.getAllMenuTree(getTenantId()));
    }

    @GetMapping(value = "/getMenutreeById/{id}")
    @ApiOperation(value = "获取包含此菜单的菜单树")
    public Result<SysTenantMenuVO> getMenutreeById(@PathVariable Long id) {
        return Result.ok(sysTenantMenuService.getMenutreeById(id, getCurrentUser()));
    }

    @PostMapping(value = "/tree/user")
    @ApiOperation(value = "pc 与人员绑定的菜单树")
    public Result<List<SysTenantMenuVO>> treeByUser(
            @RequestBody SysTenantMenuDTO dto) {
        return Result.ok(sysTenantMenuService.treeByUser(dto, getCurrentUser()));
    }

}